/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.common.client.components;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.elf.gwt.common.client.CommonComponentController;
import com.elf.gwt.common.client.access.RequestParameter;
import com.google.gwt.core.client.GWT;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.Window;


/**
 * 
 */

public class CommonMultipleResultsComponent extends CommonSimpleComponent
{
	protected List<CommonSimpleComponent> components;
	//protected Date dateLaunch = null;

	/**
	 * Constructors
	 */
	public CommonMultipleResultsComponent(CommonComponentController controller)
	{
		super(controller, true);
		setupTimer();
		components = new ArrayList<CommonSimpleComponent>();
	}

	/**
	 * This is a request method
	 */

	public void build()
	{
		if (!inuse)
		{
			CommonComponentController.enableWait();
			inuse = true;
			timer.schedule(60000); //run in 1 minute

			String handles = "";
			boolean first = true;
			Iterator<CommonSimpleComponent> iter = components.iterator();
			while(iter.hasNext())
			{
				CommonSimpleComponent component = iter.next();
				String handle = "";
				if (component != null) handle = component.getHandle();

				if (component.isEnableJSONServerAccess()) {

					if (first) {
						first = false;
						handles = handle;
					}
					else {
						handles = handles + "," + handle;
					}
				}
			}
			GWT.log("CommonMultipleResultsComponent.build handles: " + handles);
			if (handles.length() > 0)
			{
				buildRequestParameters();
				addRequestParameter(new RequestParameter("handles", handles));
				send(getRequestParameters());
			}
			else
			{
				JSONValue jsonValue = null;
				build(jsonValue);
			}
		}
	}

	public void additionalRequestParameters()
	{
		GWT.log("CommonMultipleResultsComponent additionalRequestParameters start");
		Iterator<CommonSimpleComponent> iter = components.iterator();
		while(iter.hasNext())
		{
			CommonSimpleComponent component = iter.next();
			GWT.log("CommonMultipleResultsComponent additionalRequestParameters " + component.getHandle());
			if (component.isEnableJSONServerAccess()) {
				component.clearRequestParameters();
				component.additionalRequestParameters();
				List<RequestParameter> lst = component.getRequestParameters();
				Iterator<RequestParameter> iter1 = lst.iterator();
				if (iter1 != null) {
					while (iter1.hasNext())
					{
						RequestParameter rp = iter1.next();
						addRequestParameter(rp);
					}
				}
			}
		}
		GWT.log("CommonMultipleResultsComponent additionalRequestParameters end");
	}

	public void build(JSONValue jsonValue)
	{
		controller.hideComponents();
		Iterator<CommonSimpleComponent> iter = components.iterator();
		while(iter.hasNext())
		{
			CommonSimpleComponent component = iter.next();
			if (component.isEnableJSONServerAccess()) {
				try {
					component.build(jsonValue);
				}
				catch (Exception e) {}
			}
			else {
				component.build();
			}

		}
		timer.cancel();
		inuse = false;
		CommonComponentController.disableWait();
		Window.scrollTo(0, 0);
	}

	public List<CommonSimpleComponent> getComponents() {
		return components;
	}

	public void clearComponents() {
		this.components.clear();
	}

	public void addComponent(CommonSimpleComponent component) {
		if (components.contains(component)) this.components.remove(component);
		this.components.add(component);
	}

	public void insertComponent(CommonSimpleComponent component, int index) {
		if (components.contains(component)) this.components.remove(component);
		this.components.add(index, component);
	}

	public void removeComponent(CommonSimpleComponent component) {
		if (components.contains(component))
			this.components.remove(component);
	}
}
